<template>
  <view class="detail" v-if="detailObj.goods_name">
    <swiper class="swiper" :indicator-dots="true" :autoplay="true" :circular="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item,index) in detailObj.pics" :key="index">
        <image @click="preImage(index)" :src="item.pics_big_url" mode=""></image>
      </swiper-item>
    </swiper>
    <view class="infoBox">
      <view class="price">
        ¥{{detailObj.goods_price}}
      </view>
      <view class="info">
        <view class="left">
          {{detailObj.goods_name}}
        </view>
        <view class="right">
          <uni-icons type="star" size="20"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
    </view>
    <view class="pass">
      快递:免邮费
    </view>
    <!-- 商品介绍 -->
    <rich-text :nodes="detailObj.goods_introduce"></rich-text>
    <!-- 商品导航 -->
    <uni-goods-nav class='goods_nav' @click="myclick" @buttonClick="mybuttonclick" :options="options" :fill="true"
      :buttonGroup="buttonGroup"></uni-goods-nav>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    computed: {
      ...mapState('cart', ['test']),
      ...mapGetters('cart', ['total'])
    },
    data() {
      return {
        detailObj: {},
        goods_id: '',
        options: [{
          text: '店铺',
          icon: 'shop'
        }, {
          text: '购物车',
          icon: 'cart',
          info: 0,
          infoBackgroundColor: '#ff0101',
          infoColor: '#fff'
        }],
        buttonGroup: [{
          text: '加入购物车',
          backgroundColor: '#f00000',
          color: '#fff'
        }, {
          text: '立即购买',
          backgroundColor: '#ffa200',
          color: '#fff'
        }]
      }
    },
    onLoad(options) {
      this.goods_id = options.goods_id
      this.getDetail()
    },
    watch: {
      'total': {
        handler: function(newV) {
          this.options[1].info = newV
        },
        immediate: true // 打开页面，立即调用这个侦听器
      }
    },
    methods: {
      ...mapMutations('cart', ['addToCart']),
      //左侧点击
      myclick(e) {
        if (e.index === 1) {
          // 跳转到购物车中
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      //右侧点击
      mybuttonclick(e) {
        if (e.index === 0) {
          this.addToCart({
            goods_id: this.detailObj.goods_id,
            goods_name: this.detailObj.goods_name,
            goods_status: true,
            goods_price: this.detailObj.goods_price,
            goods_count: 1,
            goods_small_logo: this.detailObj.goods_small_logo,
          })
        }
        // console.log(e);
      },
      //图片放大
      preImage(index) {
        let urls = this.detailObj.pics.map(item => {
          return item.pics_big_url
        })
        uni.previewImage({
          urls: urls,
          curretn: index
        })
      },
      async getDetail() {
        let {
          data: res
        } = await uni.$http.get(`/api/public/v1/goods/detail?goods_id=${this.goods_id}`)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.detailObj = res.message
        this.detailObj.goods_introduce = this.detailObj.goods_introduce.replace(/<img /g,
          '<img style="display:block;" ')
        this.detailObj.goods_introduce = this.detailObj.goods_introduce.replace(/.webp/g,
          '.jpg')
      }
    }
  }
</script>

<style lang="scss">
  .detail {
    padding-bottom: 50px;

    .swiper {
      height: 750rpx;

      image {
        height: 750rpx;
        width: 100%;
      }
    }

    .infoBox {
      padding: 0 20rpx;

      .price {
        color: #c30d02;
        font-size: 24px;
        line-height: 100rpx;
      }

      .info {
        display: flex;
        font-size: 15px;
        justify-content: space-between;

        .left {
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
        }

        .right {
          border-left: 1px solid #ccc;
          width: 300rpx;
          justify-content: center;
          display: flex;
          align-items: center;
          flex-direction: column;

        }
      }
    }

    .pass {
      line-height: 60rpx;
      font-size: 12px;
      color: #8b949f;
    }

    .goods_nav {
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
    }
  }
</style>
